﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super_Wall_Breaker.Sources.GameElements.Items;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Super_Wall_Breaker.Sources.Factories;


namespace Super_Wall_Breaker.Sources.GameElements.Bricks
{
    public abstract class Brick : Sprite
    {
        ///////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////// ATTRIBUTS & PROPS ///////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////

        private int _initLife;
        protected int _currentLife;

        private int _maxInvincibilityTime = 1000; //in ms
        private int _currentInvincibilityTime = 0; // in ms


        /// <summary>
        /// Gets or sets the init life.
        /// </summary>
        /// <value>
        /// The init life.
        /// </value>
        public int InitLife
        {
            get { return this._initLife; }
            set { this._initLife = value; }
        }

        /// <summary>
        /// Gets the current life.
        /// </summary>
        public int CurrentLife
        {
            get { return this._currentLife; }
        }

        private Item _item;

        /// <summary>
        /// Gets or sets the item.
        /// </summary>
        /// <value>
        /// The item.
        /// </value>
        public Item Item
        {
            get { return _item; }
            set { _item = value; }
        }
        private int _defaultBrickScore = 10;

        /// <summary>
        /// Gets or sets the default brick scrore.
        /// </summary>
        /// <value>
        /// The default brick scrore.
        /// </value>
        public int DefaultBrickScrore
        {
            get { return _defaultBrickScore; }
            set { _defaultBrickScore = value; }
        }        

        private int _valueScore;

        /// <summary>
        /// Gets or sets the value score.
        /// </summary>
        /// <value>
        /// The value score.
        /// </value>
        public int ValueScore
        {
            get { return _valueScore; }
            set { _valueScore = value; }
        }

        private Sprite _break;

        /// <summary>
        /// Gets or sets the break.
        /// </summary>
        /// <value>
        /// The break.
        /// </value>
        public Sprite Break
        {
            get { return _break; }
            set { _break = value; }
        }       


        //////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////// CONSTRUCTORS //////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////

        public Brick() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="Brick"/> class.
        /// </summary>
        /// <param name="initLife">The init life.</param>
        /// <param name="position">The position of the brick.</param>
        /// <param name="sourceRectangle">The source rectangle.</param>
        public Brick(int initLife, Vector2 position, Rectangle? sourceRectangle) :base(position,sourceRectangle)
        {
            this._initLife = initLife;   
        }

        /////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////// METHODS ///////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////

        public abstract void Update(ContentManager c);

        /// <summary>
        /// Updates the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public void Update(GameTime gameTime)
        {
            this._currentInvincibilityTime = this._currentInvincibilityTime - gameTime.ElapsedGameTime.Milliseconds;
        }

        public void IsTouched()
        {
            if (this._currentInvincibilityTime <= 0)
            {
                this._currentInvincibilityTime = this._maxInvincibilityTime;
                this._currentLife -= 1;
            }
        }

        /// <summary>
        /// Draws the specified sprite batch.
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            Color color = BrickFactory.GetBrickColor(this._initLife);
            this.Color = color;
            this.Break.Color = color;

            if (this._currentInvincibilityTime > 0)
            {
                this.Color = Color.Gold;
                this.Break.Color = Color.Gold;
            }

            base.Draw(spriteBatch);
        }
    }
}
